package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.QuoteException;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import com.qiniu.common.QiniuException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    //套餐管理

    @Reference
    private SetMealService setMealService;

    //分页查询套餐数据
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setMealService.findPage(queryPageBean);

    }

    //查询单个套餐
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        return setMealService.findById(id);
    }

    //根据套餐id查询关联表中检查组的id数据
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam("id") Integer id) {
        try {
            List<Integer> checkGroupIdsBySetmealId = setMealService.findCheckGroupIdsBySetmealId(id);

            return new Result(true, "回显检查组id成功", checkGroupIdsBySetmealId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "回显检查组id失败");
    }

    /*@Autowired
    private JedisPool jedisPool;*/
    //上传图片
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        /*  方式二  dubbo 以小数据传输,避免造成线程堵塞所以直接栽控制层上传即可
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("bytes", multipartFile.getBytes());
            map.put("name", multipartFile.getOriginalFilename());
            return setMealService.upload(map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }*/
        String fileName = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename(); //原始文件名
            //文件后缀名
            String fixName = originalFilename.substring(originalFilename.lastIndexOf("."));
            fileName = UUID.randomUUID().toString().replace("-", "") + fixName;
            //上传图片到七牛云
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(), fileName);
           QiniuUtils.deleteFileFromQiniu(fileName,1);
        } catch (QiniuException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {

          return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setMealService.add(setmeal, checkgroupIds);
        } catch (QuoteException e) {
            //添加套餐失败
            return new Result(false, "图片,套餐内容,检查组不能为空");
        } catch (Exception e) {
            return new Result(false, "添加新套餐失败");
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //删除套餐
    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        try {
            setMealService.delete(id);
        } catch (Exception e) {
            return new Result(false, "删除套餐失败");
        }
        return new Result(true, "删除套餐成功");
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setMealService.edit(setmeal, checkgroupIds);
        } catch (QuoteException e) {
            return new Result(false, "套餐内容或检查组内容不能为空");
        }catch (Exception e){
            return new Result(false, "编辑套餐失败");
        }
        return new Result(true, "编辑套餐成功");

    }

}
